package com.ruoyi.web.controller.race;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.domain.Games;
import com.ruoyi.system.domain.vo.EchartsVO;
import com.ruoyi.system.service.IGamesService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Race;
import com.ruoyi.system.service.IRaceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 比赛项目Controller
 *
 * @author ruoyi
 * @date 2024-01-22
 */
@RestController
@RequestMapping("/rac/race")
public class RaceController extends BaseController {
    @Autowired
    private IRaceService raceService;

    @Autowired
    private IGamesService gamesService;

    /**
     * 查询比赛项目列表
     */
    @PreAuthorize("@ss.hasPermi('rac:race:list')")
    @GetMapping("/list")
    public TableDataInfo list(Race race) {
        startPage();
        List<Race> list = raceService.selectRaceList(race);
        return getDataTable(list);
    }

    /**
     * 首页echarts图数据
     */
    @GetMapping(value = "/part")
    public AjaxResult getProjectPart() {
        AjaxResult ajaxResult = AjaxResult.success();
        List<Race> raceList = raceService.list(Wrappers.<Race>lambdaQuery().eq(Race::getIsOver, 0));
        List<String> raceNameList = new ArrayList<>();
        List<Integer> maxPeople = new ArrayList<>();
        List<Integer> nowPeople = new ArrayList<>();
        List<EchartsVO> echartsList = new ArrayList<>();
        raceList.forEach(race -> {
            raceNameList.add(race.getRaceName());
            maxPeople.add(race.getMaxNum());
            nowPeople.add(race.getPartNum());
            EchartsVO echartsVO = new EchartsVO();
            echartsVO.setValue(race.getPartNum());
            echartsVO.setName(race.getRaceName());
            echartsList.add(echartsVO);
        });
        ajaxResult.put("raceName", raceNameList);
        ajaxResult.put("maxPeople", maxPeople);
        ajaxResult.put("nowPeople", nowPeople);
        ajaxResult.put("echarts", echartsList);
        return success(ajaxResult);
    }

    /**
     * 导出比赛项目列表
     */
    @PreAuthorize("@ss.hasPermi('rac:race:export')")
    @Log(title = "比赛项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Race race) {
        List<Race> list = raceService.selectRaceList(race);
        ExcelUtil<Race> util = new ExcelUtil<Race>(Race.class);
        util.exportExcel(response, list, "比赛项目数据");
    }

    /**
     * 获取比赛项目详细信息
     */
    @PreAuthorize("@ss.hasPermi('rac:race:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        Race race = raceService.getById(id);
        // 根据gameId获取name
        Games games = gamesService.getById(race.getGameId());
        race.setGameName(games.getGameName());
        return success(race);
    }

    @GetMapping(value = "/game")
    public AjaxResult applyRace() {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("gameList", raceService.getGameList());
        return ajax;
    }

    /**
     * 学生报名参赛项目
     */
    @PreAuthorize("@ss.hasPermi('rac:race:apply')")
    @GetMapping(value = "/apply/{raceId}")
    public AjaxResult applyRace(@PathVariable("raceId") Integer raceId) {
        raceService.applyRace(raceId);
        return success();
    }

    /**
     * 新增比赛项目
     */
    @PreAuthorize("@ss.hasPermi('rac:race:add')")
    @Log(title = "比赛项目", businessType = BusinessType.INSERT)
    @PostMapping    public AjaxResult add(@RequestBody Race race) {
        return toAjax(raceService.insertRace(race));
    }

    /**
     * 修改比赛项目
     */
    @PreAuthorize("@ss.hasPermi('rac:race:edit')")
    @Log(title = "比赛项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Race race) {
        return toAjax(raceService.updateRace(race));
    }

    /**
     * 删除比赛项目
     */
    @PreAuthorize("@ss.hasPermi('rac:race:remove')")
    @Log(title = "比赛项目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(raceService.deleteRaceByIds(ids));
    }
}
